from django.shortcuts import render_to_response
from django.http import HttpResponse
from books.models import Book

# Create your views here.


def thank(request):
    return render_to_response('thank.html')

def search(request):
    errors = []
    if 'q' in request.GET:
        q = request.GET['q']
        if not q:
            errors.append('Enter a search term')    
        elif len(q) > 5:
            errors.append('Please submit a search term 5 characters or shorter')
        else:
            books = Book.objects.filter(title__icontains=q)
            return render_to_response('search_results.html',{'books':'books','query':q})
    return render_to_response('search_form.html',{'errors': errors})